package com.bcx.crm.service.docTemplateService;

import com.bcx.crm.mapper.docTemplateMapper.InsertDocTemplateMapper;
import com.bcx.crm.plat.core.utils.SpringContextHolder;
import com.bcx.crm.plat.core.utils.StringUtils;
import com.bcx.crm.plat.util.PinYinConvert;
import com.bcx.crm.plat.util.UtilTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * Created by ys on 2018/3/31.
 */
@Service
public class InsertDocTemplateServiceImpl implements InsertDocTemplateService {

    private static final String TEMPLATE_NAME = "templateName";
    private static final String TEMPLATE_NAME_PINYIN = "templateNamePinyin";
    private static final String INSERT_DOC_TEMPLATE_MAPPER = "insertDocTemplateMapper";

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.READ_COMMITTED)
    public boolean insertDocTemplate(Map<String, Object> map) {
        String templateName =(String) map.get(TEMPLATE_NAME);
        if (UtilTool.isValidString(templateName)){
            map.put(TEMPLATE_NAME_PINYIN, PinYinConvert.getPinYin(templateName));
        }
        map.put("rowId", UUID.randomUUID().toString());
        map.put("createTime",UtilTool.sdf.format(new Date()));
        Map<String, Object> mapLast = StringUtils.columnAddUnderline(map);
        InsertDocTemplateMapper insertDocTemplateMapper = SpringContextHolder.getBean(INSERT_DOC_TEMPLATE_MAPPER);
        return insertDocTemplateMapper.insertDocTemplate(mapLast);
    }
}
